var messageNum = 0
var BASE_URL = 'http://localhost:3000'

function login() {
  let username = document.getElementById('username').value
  let password = document.getElementById('password').value
  let remember = document.getElementById('remember').checked
  if (!username) {
    message('请先填写用户名', 'warning')
    return
  }
  if (!password) {
    message('密码不能为空', 'warning')
    return
  }
  let params = {
    username: username,
    password: password
  }
  http.post('/api/user/login', params).then(function (res) {
    if (res.code === 200) {
      message(res.message, 'success')
    } else {
      message(res.message, 'error')
    }
  }).catch(function (err) {
    message(err.message, 'error')
  })
}

function message(msg, type) {
  let messageBox = document.createElement('div')
  if (type === 'success') {
    messageBox.className = 'message message-success'
  } else if (type === 'warning') {
    messageBox.className = 'message message-warning'
  } else if (type === 'error') {
    messageBox.className = 'message message-error'
  } else {
    messageBox.className = 'message message-info'
  }
  messageBox.innerHTML = msg
  if (messageNum !== 0) {
    messageBox.style.top = 100 + messageNum * 50 + 'px'
  }
  messageNum++
  document.body.appendChild(messageBox)
  let timeOutId = setTimeout(function () {
    messageBox.remove()
    messageNum--
    clearTimeout(timeOutId)
  }, 3000)
}

var http = {
  get: function (url, params) {
    return new Promise((resolve, reject) => {
      var xhr = null
      if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest()
      } else {
        xhr = new ActiveXObject("Microsoft.XMLHTTP")
      }
      if (!xhr) {
        message('您的浏览器不支持ajax请求', 'error')
      }
      if (params) {
        let query = []
        for (let k in params) {
          query.push(k + '=' + params[k])
        }
        let qs = query.join('&')
        url += (url.indexOf('?') < 0 ? '?' : '&') + qs
      }
      xhr.open('get', BASE_URL + url, true)
      xhr.send()
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
          var res = xhr.responseText
            res = JSON.parse(res)
            resolve(res)
        }
      }
    })
  },
  post: function (url, req) {
    return new Promise((resolve, reject) => {
      let qs = ''
      var xhr = null
      if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest()
      } else {
        xhr = new ActiveXObject("Microsoft.XMLHTTP")
      }
      if (!xhr) {
        message('您的浏览器不支持ajax请求', 'error')
      }
      if (req) {
        let query = []
        for (k in req) {
          query.push(k + '=' + req[k])
        }
        qs = query.join('&')
      }
      xhr.open('post', BASE_URL + url, true)
      xhr.Origin = 'http://127.0.0.1:3001'
      xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded")
      xhr.send(qs)
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4 && xhr.status == 200) {
          var res = xhr.responseText
            res = JSON.parse(res)
            resolve(res)
        }
      }
    })
  }
}
